﻿using MediatR;
using System;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.BatchManagementDTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.BatchManagements
{
    /// <summary>
    /// 皮重列表显示+查询。。
    /// </summary>
    public class TaresetListHander : IRequestHandler<TareSetListCommand, PageDTO<TareListDTO>>
    {
        private readonly IBateRepository<TareSet> tareSet;
        private readonly IBateRepository<Shopping> shop;

        public TaresetListHander(IBateRepository<TareSet> tareSet, IBateRepository<Shopping> shop)
        {
            this.tareSet = tareSet;
            this.shop = shop;
        }

        public async Task<PageDTO<TareListDTO>> Handle(TareSetListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                PageDTO<TareListDTO> res = new PageDTO<TareListDTO>();
                var list = tareSet.GetAll();
                if (request.ShoppingDesignationid>-1)
                {
                    list = list.Where(x => x.ShoppingDesignationid == request.ShoppingDesignationid);
                }
                if (request.Stact>-1)
                {
                    list = list.Where(x => x.Stact == request.Stact);
                }
                var listDto = from a in list
                              join b in shop.GetAll() on a.ShoppingDesignationid equals b.ShoppingDesignationid
                              select new TareListDTO
                              {
                                  TareSetId=a.TareSetId,
                                  ShoppingDesignationName =b.ShoppingDesignationName,
                                  Weight=a.Weight,
                                  Unit=b.Unit,
                                  Stact=a.Stact==0?"启用":"禁用",
                                  MakeASingle = a.MakeASingle,
                                  CrateTime=a.CrateTime,
                                  UpdateASingle = a.UpdateASingle,
                                  UpdateTime = a.UpdateTime,
                                  Rarme = a.Rarme
                              };

                res.totalCount= listDto.Count();
                var resss = res.totalCount * 1.0 / request.PageSize;
                res.pageCount = (int)Math.Ceiling(resss);
                listDto = listDto.OrderBy(x=>x.TareSetId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize);
                res.Lists = listDto.ToList();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
